﻿using DBServer.Selenium.Extenium.Exception;
using DBServer.Selenium.Extenium.Path;
using Selenium;

namespace DBServer.Selenium.Extenium.Fixtures
{
    public class TreePanelFixture : ExtComponentFixture
    {

        public TreePanelFixture(ISelenium driver, IPath path)
            : base(driver, path)
        { 
        }

        public void Click()
        {
            SafeExec(Path.Eval() + ".btnEl.dom.click();");
        }

        public TreePanelFixture RequireContainsText(string text)
        {
            var cmd = "  var cmp = " + Path.Eval() + ";" + 
                      "  var text = '" + text + "';" +
                      "  var node = cmp.root; " +
                      "  function contem(){ " +
                      "  for (var i = 0; i<node.childNodes.length; i++){ " +
                      "      if (node.childNodes[i].text.indexOf(text) != -1) return true; " +
                      " } " +
                      "  return false;}" +
                      "contem();  ";

            Wait(() => (bool.Parse(SafeExec(Path.Eval() + ".root.loaded"))));
            var ret = bool.Parse(this.SafeExec(cmd));
            if(!ret)
                throw new ExteniumException("The " + text + " was not found.");

            return this;
        }
    }
}